package com.quanquan.management.controller;

import base.BaseConst;
import base.BaseController;
import com.quanquan.management.dto.UserDisCountDTO;
import com.quanquan.management.request.UserDisCountRequest;
import com.quanquan.management.service.UserDisCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName:UserDisCountController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/12/30 18:13
 * @Version 1.0
 */
@Controller
@RequestMapping("/user_discount")
public class UserDisCountController extends BaseController {

    private final UserDisCountService userDisCountService;

    @Autowired
    public UserDisCountController(UserDisCountService userDisCountService) {
        this.userDisCountService = userDisCountService;
    }

    /**
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param isAsc
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) boolean isAsc,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        UserDisCountRequest requestBody = new UserDisCountRequest(page, pageSize, keyword, sort, isAsc);
        UserDisCountDTO pageDTO = userDisCountService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "userDiscount/list";
    }


    /**
     * 更新会员权益（停车）
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(String id, UserDisCountRequest requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            UserDisCountDTO byId = userDisCountService.findById();
            model.addAttribute("editable", byId);
            return "userDiscount/form";
        }
        boolean update = userDisCountService.update(requestBody);
        if (update) {
            return "redirect:/user_discount/list";
        }
        return null;
    }

}
